import { defineStore } from "pinia"
import { TagView } from '#/setting'

export interface TagsViewState {
  showTagsView: boolean  // 是否显示tags
  visitedViews: TagView[] // 当前页面的tagsViews
}

export const settingStore = defineStore({
  id: 'app-setting',
  state: (): TagsViewState => ({
    showTagsView: true,
    visitedViews: [], 
  }),
  actions: {
    toChangeTags(isShow: boolean) {
      this.showTagsView = isShow
    },
    /**
     * 新加头部标签
     */
    ADD_VISITED_VIEW(view: TagView) {
      if (this.visitedViews.some(v => v.path === view.path)) return
      this.visitedViews.push(
        Object.assign({}, view, {
          title: view.meta?.title || 'no-name'
        })
      )
    },
    /**
     * 删除标签
     */
    DEL_VISITED_VIEW(view: TagView) {
      for (const [i, v] of this.visitedViews.entries()) {
        if (v.path === view.path) {
          this.visitedViews.splice(i, 1)
          break
        }
      }
    }
  }
})